<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>19.为什么子类constructor中执行super</title>
</head>

<body>

</body>
<script>
  //在构造函数`constructor` 中必须先调用`super()

  class User {
    constructor(name) {
      this.name = name;
    }
    show () {
      console.log(this.name);
    }
  }
  class Admin extends User {
    constructor(...args) {
      super(...args);// 如果写出constructor，必须调用super 否则父类的构造函数将不会被调用
      this.name = "houdunren.com" // this 调用要放在super后，试试强制要求，否则父类的同名属性（name）可能覆盖子类
    }
  }
  let hd = new Admin("后盾人");
  hd.show(); // houdunren.com
  // 原理

  function Parent (name) {
    this.name = name;
  }
  function User2 (...args) {
    Parent.apply(this, args); //这里的apply相当于 super()
  }
  User2.prototype = Object.create(Parent.prototype)
  User2.prototype.constructor = User2;
  const hd2 = new User2("后盾人");
  console.log(hd2);
  console.log(hd2.name);
</script>

</html>